package com.ss.android.common.util;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Build;

import com.ss.android.common.AppContext;

public class ToolUtils {
	public static void addImageMeida(Context context,String str){
		try{
			new MyMediaScannerConnectionClient(context,str).startScan();
		}catch(Exception e){
			
		}
	}
	
	public static void clearDir(String str) throws Exception{
		/*if(!Environment.getExternalStorageState().equals("mounted")){
			File file=new File(str);
			if(file.exists()){
				File[] arrFile=file.listFiles();
				int i=arrFile.length;
				int j=0;
				if(j<i){
					if(!arrFile[j].isDirectory()){
						
					}
				}
			}
		}*/
	}
	
	public static String getCommonApiParams(AppContext context){
		return getCommonApiParams(context,false);
	}
	
	public static String getCommonApiParams(AppContext context,boolean bool){
		StringBuilder builder=new StringBuilder();
		if(!bool){
			builder.append("platform=android");
		}
		builder.append("&device_platform=android");
		if(context==null){
			return builder.toString();
		}
		String channel=context.getChannel();
		if(channel!=null){
			builder.append("&channel=").append(Uri.encode(channel));
		}
		builder.append("&version_code=").append(context.getVersionCode());
		String appName=context.getAppName();
		if(appName!=null){
			builder.append("&app_name=").append(Uri.encode(appName));
		}
		builder.append("&device_type=").append(Uri.encode(Build.MODEL));
		builder.append("&os_version=").append(Uri.encode(Build.VERSION.RELEASE));
		return builder.toString();
	}
	
	private static class MyMediaScannerConnectionClient
		implements MediaScannerConnection.MediaScannerConnectionClient{
		private MediaScannerConnection conn;
		private Context mContext;
		private String mImagePath;
		
		public MyMediaScannerConnectionClient(Context context,String imagePath){
			this.mContext=context;
			this.mImagePath=imagePath;
		}
		
		public void onMediaScannerConnected(){
			try{
				this.conn.scanFile(this.mImagePath, "image/*");
			}catch(Exception e){
				
			}
		}
		
		public void onScanCompleted(String str,Uri uri){
			if(this.conn!=null){
				if(this.conn.isConnected()){
					this.conn.disconnect();
				}
				this.conn=null;
			}
		}
		
		public void startScan(){
			if((this.conn!=null)&&(this.conn.isConnected())){
				this.conn.disconnect();
			}
			this.conn=new MediaScannerConnection(this.mContext,this);
			this.conn.connect();
		}
	}
}
